package models_sports

import (
	"encoding/json"
	"fmt"
	"longmen/server/config/global"
)

// CricketMatch 赛程
type CricketMatch struct {
	ID                 int                `json:"id" gorm:"primaryKey; comment:比赛id "`
	UniqueTournamentID int                `json:"unique_tournament_id" gorm:"type:int(11);comment:赛事id"`
	SeasonID           int                `json:"season_id" gorm:"type:int(11);comment:赛季id"`
	TournamentID       int                `json:"tournament_id" gorm:"type:int(11);comment:阶段id"`
	VenueID            int                `json:"venue_id" gorm:"type:int(11);comment:场馆id"`
	HomeTeamID         int                `json:"home_team_id" gorm:"type:int(11);comment:主队id"`
	AwayTeamID         int                `json:"away_team_id" gorm:"type:int(11);comment:客队id"`
	MatchTime          int64              `json:"match_time" gorm:"type:bigint(20);comment:比赛时间"`
	StatusID           CricketMatchStatus `json:"status_id" gorm:"type:int(11);comment:比赛状态"`
	Neutral            int                `json:"neutral" gorm:"type:int(11);comment:是否中立场，1-是、0-否"`
	Mlive              int                `json:"mlive" gorm:"type:int(11);comment:是否有动画，1-是、0-否"`
	Lineup             int                `json:"lineup" gorm:"type:int(11);comment:是否有阵容，1-是、0-否"`
	Scores             string             `json:"scores" gorm:"type:varchar(255);comment:比分数据"`
	ExtraScores        string             `json:"extra_scores" gorm:"type:varchar(255);comment:小局详情与获胜方式（不存在为空）"`
	Weather            string             `json:"weather" gorm:"type:varchar(255);comment:天气"`
	UpdatedAt          int                `json:"updated_at" gorm:"type:int(11);comment:更新时间"`
}

type CricketMatchStatus uint32

type CricketMatchWeather struct {
	Desc     string `json:"desc"`
	Temp     int    `json:"temp"`
	Wind     int    `json:"wind"`
	Humidity int    `json:"humidity"`
	Pressure int    `json:"pressure"`
}

func (receiver CricketMatchWeather) String() string {
	s, err := json.Marshal(&receiver)
	if err != nil {
		global.Log.Warnf("failed to marshal whether: %s", err.Error())
		return ""
	}
	return string(s)
}

func (ms CricketMatchStatus) ToString() string {
	statusMap := map[uint32]string{
		0:   "比赛异常",
		1:   "未开赛",
		532: "第一局主队",
		533: "第一局客队",
		534: "第二局主队",
		535: "第二局客队",
		536: "等待超级轮",
		537: "超级轮主队",
		538: "超级轮客队",
		539: "超级轮结束",
		540: "局中休息",
		541: "超级轮休息",
		542: "午休",
		543: "茶歇",
		544: "结束(当日比赛)",
		100: "完场",
		14:  "延期",
		15:  "推迟",
		16:  "取消",
		17:  "中断",
		19:  "腰斩",
		99:  "待定",
	}
	s, ok := statusMap[uint32(ms)]
	if !ok {
		return fmt.Sprintf("未知:%d", uint32(ms))
	}
	return s
}
func (m CricketMatch) GetStartingStatus() []int {
	return []int{1,
		532, 533, 534, 535, 536, 537, 538, 539,
		540, 541, 542, 543, 544}
}

func (ms CricketMatchStatus) MatchTempScheduleStatus() uint32 {
	// tips: 0 作为 异常, 与 足球 不同.
	// 比赛状态, 1-进行,2-未开赛,3-完场
	if ms == 1 || ms == 99 {
		return 2 // 2-未开赛
	}
	if ms == 100 {
		return 3 // 3-完场
	}
	if ms == 0 || (ms >= 14 && ms <= 19) {
		return 4 // 4-异常
	}
	if ms >= 532 && ms <= 545 {
		return 1 // 1-进行
	}

	return 0 // 未知 ?
}
